package com.jrx.anytxn.param.controller.product;

import com.jrx.anytxn.common.exception.TxnException;
import com.jrx.anytxn.i18n.response.TxnPage;
import com.jrx.anytxn.i18n.response.TxnRespResult;
import com.jrx.anytxn.param.dto.product.PrWaiveInterestTableReq;
import com.jrx.anytxn.param.dto.product.PrWaiveInterestTableRes;
import com.jrx.anytxn.param.plugins.DeleteLog;
import com.jrx.anytxn.param.plugins.DeleteLogHelper;
import com.jrx.anytxn.param.service.product.IPrWaiveInterestTableService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import jrx.auth.filter.service.InfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.math.BigDecimal;

/**
 * 贴息利率表controller
 *
 * @author yf
 * date 2020/2/24 0024
 * @version V1.0
 **/
@Api(tags = "页面贴息利率参数API接口")
@RestController
@RequestMapping(value = "/param/waiveInterest")
public class PrWaiveInterestTableController {

    private static final Logger logger = LoggerFactory.getLogger(PrWaiveInterestTableController.class);
    @Autowired
    IPrWaiveInterestTableService iPrWaiveInterestTableService;
    @Autowired
    private InfoService infoService;

    @ApiOperation(value = "根据租户ID和贴息利率表ID查询贴息利息表信息", notes = "根据租户ID和贴息利率表ID查询贴息利息表信息")
    @GetMapping(value = "/{tenantId}/{interestId}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tenantId", value = "租户ID", required = true),
            @ApiImplicitParam(name = "interestId", value = "贴息利率表ID", required = true)
    })
    public TxnRespResult<PrWaiveInterestTableRes> getByTenIdAndInterestId(@NotNull @PathVariable String tenantId, @PathVariable String interestId) {
        TxnRespResult<PrWaiveInterestTableRes> result = new TxnRespResult<>();
        PrWaiveInterestTableRes res;
        try {
            res = iPrWaiveInterestTableService.findByTenIdAndInterestId(String.valueOf(infoService.getUser().getProjectId()), interestId);
            if (res.getBaseRate() != null && res.getBaseRate().compareTo(BigDecimal.valueOf(0)) != 0) {
                res.setBaseRate(res.getBaseRate().multiply(BigDecimal.valueOf(100)));
            }
            if (res.getTaxRate() != null && res.getTaxRate().compareTo(BigDecimal.valueOf(0)) != 0) {
                res.setTaxRate(res.getTaxRate().multiply(BigDecimal.valueOf(100)));
            }
            result.getSuccess(res);
        } catch (TxnException e) {
            logger.error(e.getMessage(), e);
            result.getFail(e.getErrCode(), e.getMessage());
        }
        return result;
    }

    @ApiOperation(value = "分页查询贴息利息表信息", notes = "分页查询贴息利息表信息")
    @GetMapping(value = "/pageNum/{pageNum}/pageSize/{pageSize}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页码", required = true),
            @ApiImplicitParam(name = "pageSize", value = "页大小", required = true)})
    public TxnRespResult<TxnPage<PrWaiveInterestTableRes>> getAllPage(@PathVariable(value = "pageNum") Integer pageNum, @PathVariable(value = "pageSize") Integer pageSize) {

        TxnRespResult<TxnPage<PrWaiveInterestTableRes>> result = new TxnRespResult<>();
        TxnPage<PrWaiveInterestTableRes> response;
        String tenantId = String.valueOf(infoService.getUser().getProjectId());
        try {
            response = iPrWaiveInterestTableService.findAllPageByTenId(pageNum, pageSize, tenantId);
            result.getSuccess(response);
        } catch (TxnException e) {
            logger.error(e.getMessage(), e);
            result.getFail(e.getErrCode(), e.getMessage());
        }
        return result;
    }


    @ApiOperation(value = "新增贴息利息表信息", notes = "新增贴息利息表信息")
    @PostMapping("/")
    public TxnRespResult<PrWaiveInterestTableRes> createWaiveInterestTable(@RequestBody PrWaiveInterestTableReq prWaiveInterestTableReq) {
        PrWaiveInterestTableRes res;
        TxnRespResult<PrWaiveInterestTableRes> result = new TxnRespResult<>();
        try {
            prWaiveInterestTableReq.setTenantId(String.valueOf(infoService.getUser().getProjectId()));
            prWaiveInterestTableReq.setUpdateBy(infoService.getUser().getUserName());
            if (prWaiveInterestTableReq.getBaseRate() != null && prWaiveInterestTableReq.getBaseRate().compareTo(BigDecimal.ZERO) != 0) {
                prWaiveInterestTableReq.setBaseRate(prWaiveInterestTableReq.getBaseRate().divide(BigDecimal.valueOf(100)));
            }
            if (prWaiveInterestTableReq.getTaxRate() != null && prWaiveInterestTableReq.getTaxRate().compareTo(BigDecimal.ZERO) != 0) {
                prWaiveInterestTableReq.setTaxRate(prWaiveInterestTableReq.getTaxRate().divide(BigDecimal.valueOf(100)));
            }
            res = iPrWaiveInterestTableService.addWaiveInterestTable(prWaiveInterestTableReq);
            result.getSuccess(res);
        } catch (TxnException e) {
            logger.error(e.getMessage(), e);
            result.getFail(e.getErrCode(), e.getMessage());
        }
        return result;
    }


    @ApiOperation(value = "编辑贴息利息表信息", notes = "编辑贴息利息表信息")
    @PutMapping("/")
    public TxnRespResult<PrWaiveInterestTableRes> modifyInterestTable(@RequestBody PrWaiveInterestTableReq prWaiveInterestTableReq) {
        TxnRespResult<PrWaiveInterestTableRes> result = new TxnRespResult<>();
        PrWaiveInterestTableRes res;
        try {
            prWaiveInterestTableReq.setUpdateBy(infoService.getUser().getUserName());
            if (prWaiveInterestTableReq.getBaseRate() != null && prWaiveInterestTableReq.getBaseRate().compareTo(BigDecimal.ZERO) != 0) {
                prWaiveInterestTableReq.setBaseRate(prWaiveInterestTableReq.getBaseRate().divide(BigDecimal.valueOf(100)));
            }
            if (prWaiveInterestTableReq.getTaxRate() != null && prWaiveInterestTableReq.getTaxRate().compareTo(BigDecimal.ZERO) != 0) {
                prWaiveInterestTableReq.setTaxRate(prWaiveInterestTableReq.getTaxRate().divide(BigDecimal.valueOf(100)));
            }
            res = iPrWaiveInterestTableService.modifyWaiveInterestTable(prWaiveInterestTableReq);
            result.getSuccess(res);
        } catch (TxnException e) {
            logger.error(e.getMessage(), e);
            result.getFail(e.getErrCode(), e.getMessage());
        }
        return result;
    }

    @ApiOperation(value = "根据tenantId 和 interestId删除贴息利息表信息", notes = "根据tenantId 和 interestId删除贴息利息表信息")
    @DeleteMapping(value = "/{tenantId}/{interestId}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tenantId", value = "租户Id", required = true),
            @ApiImplicitParam(name = "interestId", value = "贴息利率表ID", required = true)
    })
    public TxnRespResult<Boolean> removeByTenIdAndInterestId(@NotNull @PathVariable String tenantId, @PathVariable String interestId) {
        TxnRespResult<Boolean> result = new TxnRespResult<>();
        Boolean flag;
        try {
            DeleteLog deleteLog = new DeleteLog(infoService.getUser().getUserName());
            DeleteLogHelper.setDelleteHistory(deleteLog);

            flag = iPrWaiveInterestTableService.removeByTenIdAndInterestId(String.valueOf(infoService.getUser().getProjectId()), interestId);
            result.getSuccess(flag);
        } catch (TxnException e) {
            logger.error(e.getMessage(), e);
            result.getFail(e.getErrCode(), e.getMessage());
        } finally {
            DeleteLogHelper.clearLocalHistory();
        }
        return result;
    }
}
